package tc;

import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class CustJTable extends JTable {
	Vector tableContVect;
	Vector colNameVect;
	int length;
	int width;
	boolean[] flag;
	CustJTable table;
	int[] columnWidths;
	CustTableModel model = null;

	public CustJTable(Vector tableContVect, Vector colNameVect,
			int[] columnWidths) {
		this.tableContVect = tableContVect;
		this.colNameVect = colNameVect;
		this.columnWidths = columnWidths;
		this.table = this;
		initialize();
	}

	public Vector getTableContVect() {
		return tableContVect;
	}

	public void setTableContVect(Vector tableContVect) {
		this.tableContVect = tableContVect;
	}

	public CustTableModel getModel() {
		return model;
	}

	public void initialize() {
		model = new CustTableModel(this.tableContVect, this.colNameVect);
		setModel(model);

		setAutoResizeMode(0);
		for (int i = 0; i < getColumnCount(); ++i)
			getColumnModel().getColumn(i).setPreferredWidth(columnWidths[i]);
		getTableHeader().setReorderingAllowed(false);// 表列不可移动
		
		// this.table.getModel().addTableModelListener(new TableModelListener()
		// {
		// public void tableChanged(TableModelEvent e) {
		// System.out.println("按回车键改变");
		// }
		// });
		
		
//		table.setCellSelectionEnabled(true);// 使得表格的选取是以cell为单位,而不是以列为单位.若你没有写此行,则在选取表格数据时以整列为单位.
//		ListSelectionModel selectionMode = table.getSelectionModel();// 取得table的ListSelectionModel.
//		selectionMode = table.getSelectionModel();// 取得table的ListSelectionModel.
//		selectionMode.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 单一选择，否则点击释放会触发两次
//		selectionMode.addListSelectionListener(new ListSelectionListener() {
//			public void valueChanged(ListSelectionEvent e) {
//				String tempString = "";
//				boolean isMouseRealed= e.getValueIsAdjusting();//判断鼠标释放
//				int[] rows = table.getSelectedRows();
//				int[] columns = table.getSelectedColumns();
//				int row = table.getSelectedRow();
//				int column = table.getSelectedColumn();
//				tempString = (String) table.getValueAt(row, column);
//				if(!isMouseRealed){
//				//System.out.println("选择的数据为" + tempString);
//				}
//			}
//		});
		
		// this.table.addMouseListener(new MouseAdapter() {
		// public void mouseClicked(MouseEvent mouseevent) {
		// try {
		// System.out.println("鼠标点击");
		// int[] rows = getSelectedRows();
		// for (int i = 0; i < rows.length; ++i) {
		// System.out.println("选中行的值："
		// + table.getValueAt(rows[i], 1));
		// String bo = (String) table.getValueAt(rows[i], 1);
		// table.setValueAt(bo, rows[i], 1);
		// }
		// table.repaint();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }
		// });
		
	}

}